package program.commands;

import item.*;
import item.pickable.*;

/**
 * A class concerning pick-up-and-use-commands: (pick-up-and-use).
 *
 */
public class PickupAndUse extends BasicCommand{
	/**
	 * Initialise this new pick-up-and-use-command with a line number.
	 *
	 */
	public PickupAndUse(int lineNb) {
		super(lineNb);
	}
	
	/**
	 * Returns a textual representation of this pick-up-and-use-command,
	 * formatted in accordance with its position in the program.
	 *
	 * @return	
	 *            | result == getIndentation(indentationLevel) + "(pick-up-and-use)"
	 */
	@Override
	public String prettyFormat(int indentationLevel) {
		return getIndentation(indentationLevel) + "(pick-up-and-use)";
	}

	/**
	 * Execute the pickup-and-use method on a given robot.
	 *
	 * @effect      The given robot picks-up a random item (if present) on his position
	 *                      and uses it immediately.
	 *                      | ...
	 */
	@Override
	public void execute(Robot robot) throws IllegalArgumentException {
		super.execute(robot);
		PickableItem found = null;
		for(Item item: robot.getPosition().getListOfItemsAtPosition())
			if(item instanceof PickableItem) found = (PickableItem) item;
		robot.pickUp(found);
		robot.use(found);
	}
} 